package com.example.config.staticTask;

import cn.hutool.core.date.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 需要在配置类中开启 @EnableScheduling 注解
 * 带有 @Scheduled 也需要声明为 spring bean
 * @author ZhengDing
 * @date 2022/4/9 13:35
 */
@Component
public class MyStaticTask {
    
    // @Scheduled(cron = "0/5 * * * * ?")
    public void task01() {
        System.out.println(Thread.currentThread().getName());
        System.out.println("task01: [" + DateUtil.now() + "] 从 0 秒开始,每 5 秒执行一次");
    }
    
    // @Scheduled(cron = "3/18 * * * * ?")
    public void task02() {
        System.out.println(Thread.currentThread().getName());
        
        System.out.println("task02: [" + DateUtil.now() + "] 从 3 秒开始,每 18 秒执行一次");
        

    }
    
    // @Scheduled(cron = "* 21 23 * * ?")
    public void task03() {
        System.out.println(Thread.currentThread().getName());
        System.out.println("task01: [" + DateUtil.now() + "] 21:23分,每秒执行一次");
    }
    
    // @Scheduled(fixedDelay = 3000L)
    public void fixedDelayTask() {
        System.out.println(Thread.currentThread().getName()  + " FixedDelayTask ...");
    }
    
    // @Scheduled(fixedRate = 2000L)
    public void fixedRateTask() {
        System.out.println(Thread.currentThread().getName() + " FixedRateTask ...");
    }
    
    @Scheduled(fixedRate = 2000L, initialDelay = 3000L)
    public void fixedRateTaskAfterInitialTime() {
        System.out.println(Thread.currentThread().getName() + " FixedRateTask ...");
    }
}
